package com.appheader.rongmaphone.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.activity.ContentActivity;
import com.appheader.rongmaphone.adapter.OffLineAdapter;
import com.appheader.rongmaphone.cache.preference.PreferenceUtil;
import com.appheader.rongmaphone.entity.Project;
import com.appheader.rongmaphone.user.UserInfo;
import com.appheader.rongmaphone.utils.json.JSONUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 离线缓存
 */

public class OffLineFragment extends Fragment implements AdapterView.OnItemClickListener {
    private PullToRefreshGridView offLineGridView;
    private OffLineAdapter adapter;
    private List<Project> data;
    private int pageNum;
    private RelativeLayout relativeLayout;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private PreferenceUtil preferenceUtil;
    private String uid;

    public OffLineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_off_line, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.mipmap.default_img)
                .showImageForEmptyUri(R.mipmap.default_img)
                .showImageOnFail(R.mipmap.default_img).cacheInMemory(true)
                .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();

//        List<Project> all = DataSupport.findAll(Project.class);
//        DataSupport.deleteAll(Project.class);
        preferenceUtil = new PreferenceUtil(getContext());
        String userStr = preferenceUtil.getString("userStr");//从私有目录取出
        UserInfo userInfo = JSONUtil.userInfos(userStr);
        uid = userInfo.getUid();
        getDatas();
        offLineGridView.setOnItemClickListener(this);
    }
    private void getDatas() {
        List<Project> projectList = DataSupport.findAll(Project.class);
        Log.e("OffLineFragment", "getDatas: " + projectList.size());
        if (projectList.size() == 0) {
            relativeLayout.setVisibility(View.VISIBLE);
        } else {
            data = new ArrayList<>();
            for (int i = 0; i < projectList.size(); i++) {
                Project cloudEntity = new Project();
                String picId = projectList.get(i).getPicId();
                String name = projectList.get(i).getName();
                String prjectId = projectList.get(i).getPrjectId();
                String pageType = projectList.get(i).getPageType();
                String dbUid = projectList.get(i).getCreator();
                Log.e("OffLineFramgent", "getDatas: "+picId+"--"+name+"--"+prjectId+"--"+pageType+"--uid="+uid+"dbUid="+dbUid);
                if (dbUid.equals(uid)){
                    cloudEntity.setPicId(picId);
                    cloudEntity.setName(name);
                    cloudEntity.setPrjectId(prjectId);
                    cloudEntity.setPageType(pageType);
                    cloudEntity.setCreator(uid);
                    data.add(cloudEntity);
                }
            }
            adapter = new OffLineAdapter(data, getContext(),options,imageLoader);
            offLineGridView.setAdapter(adapter);
            Log.e("sdddddd", "getDatas: 走了吗" );
        }
    }

    private void initView(View view) {
        offLineGridView = (PullToRefreshGridView) view.findViewById(R.id.offline_cloud_gridview);
        offLineGridView.getRefreshableView().setNumColumns(2);//显示两列
        relativeLayout = (RelativeLayout) view.findViewById(R.id.data_layout);
        offLineGridView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH);//同时支持上拉加载，下拉刷新
    }
    //gridview的item点击监听
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //正常是点击，请求网络下载zip包到本地，跳转到详情页，在详情页解压zip包获取信息
        //判断如果下载成功，跳转到详情页，否者提示下载失败！
        String prjectId = data.get(position).getPrjectId();
        String pageType = data.get(position).getPageType();
        String creator = data.get(position).getCreator();
        String picId = data.get(position).getPicId();
        String name = data.get(position).getName();

        Intent intent = new Intent(getActivity(), ContentActivity.class);
        intent.putExtra("uid", creator);
        intent.putExtra("projectId", prjectId);
        intent.putExtra("review", "review");
        intent.putExtra("picId", picId);
        intent.putExtra("name", name);
        intent.putExtra("isOnline", false);
        intent.putExtra("pageType", pageType);
        startActivity(intent);
    }
}
